package cn.south.campus.common.file.service;

import cn.south.campus.common.config.FileUploadConfig;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Component
public class ImgJsonSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (!StringUtils.hasText(value)) {
            gen.writeString("");
            return;
        }
        String[] imgs = value.split(",");
        StringBuilder sb = new StringBuilder();
        for (String img : imgs) {
            sb.append(FileUploadConfig.DOMAIN).append(img).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        gen.writeString(sb.toString());
    }
}
